/**
 * 获取上一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
function getPreMonth(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    //var day = arr[2]; //获取当前日期的日
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中月的天数
    var year2 = year;
    var month2 = parseInt(month) - 1;
    if (month2 == 0) {
        year2 = parseInt(year2) - 1;
        month2 = 12;
    }
    //var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    //if (day2 > days2) {
    //    day2 = days2;
    //}
    if (month2 < 10) {
        month2 = '0' + month2;
    }
    var t2 = year2 + '-' + month2//+ '-' + day2;
    return t2;
}

/**
 * 获取下一个月
 *
 * @date 格式为yyyy-mm-dd的日期，如：2014-01-25
 */
function getNextMonth(date) {
    var arr = date.split('-');
    var year = arr[0]; //获取当前日期的年份
    var month = arr[1]; //获取当前日期的月份
    //var day = arr[2]; //获取当前日期的日
    var days = new Date(year, month, 0);
    days = days.getDate(); //获取当前日期中的月的天数
    var year2 = year;
    var month2 = parseInt(month) + 1;
    if (month2 == 13) {
        year2 = parseInt(year2) + 1;
        month2 = 1;
    }
    //var day2 = day;
    var days2 = new Date(year2, month2, 0);
    days2 = days2.getDate();
    //if (day2 > days2) {
    //    day2 = days2;
    //}
    if (month2 < 10) {
        month2 = '0' + month2;
    }

    var t2 = year2 + '-' + month2// + '-' + day2;
    return t2;
}
/**
 * 判断指定字符串是否存在源字符串中
 */
function isContain(subStr,sourceStr){
    var targetStr = sourceStr.split(",");
    var x ;
    for(x in targetStr){
        if(targetStr[x] == subStr){
            return true;
        }
    }
    return false;
}

/**
 * 在给定的字符串中删除指定子字符串
 * @param targetStr
 * @param sourceStr
 */
function strDel(subStr,sourceStr){
    var targetStr = sourceStr.split(",");
    var x;
    for(x in targetStr){
        if(targetStr[x] == subStr){
            targetStr.splice(x,1);
            return targetStr.join(",");
        }

    }
}

function changeTwoDecimal_f(x) {
    var f_x = parseFloat(x);
    if (isNaN(f_x)) {
        alert('function:changeTwoDecimal->parameter error');
        return false;
    }
    var f_x = Math.round(x * 100) / 100;
    var s_x = f_x.toString();
    var pos_decimal = s_x.indexOf('.');
    if (pos_decimal < 0) {
        pos_decimal = s_x.length;
        s_x += '.';
    }
    while (s_x.length <= pos_decimal + 2) {
        s_x += '0';
    }
    return s_x;
}

/**
 * 比较两个日期
 * @param startdate
 * @param enddate
 * @returns {boolean}
 */
function dateCompare(startdate,enddate) {
    var arr=startdate.split("-");
    var starttime=new Date(arr[0],arr[1],arr[2]);
    var starttimes=starttime.getTime();

    var arrs=enddate.split("-");
    var lktime=new Date(arrs[0],arrs[1],arrs[2]);
    var lktimes=lktime.getTime();

    if(starttimes<lktimes)
    {
        return false;
    }
    else
        return true;

}

//返回两个日期相隔的天数
function getDays(strDateStart,strDateEnd){
    var strSeparator = "-"; //日期分隔符
    var oDate1;
    var oDate2;
    var iDays;
    oDate1= strDateStart.split(strSeparator);
    oDate2= strDateEnd.split(strSeparator);
    var strDateS = new Date(oDate1[0], oDate1[1]-1, oDate1[2]);
    var strDateE = new Date(oDate2[0], oDate2[1]-1, oDate2[2]);
    iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24)//把相差的毫秒数转换为天数
    return iDays ;
}

//测试
//alert(getPreMonth("2014-01-25"));
//alert(getNextMonth("2014-12-25"));

/*
 *string:原始字符串
 *substr:子字符串
 *isIgnoreCase:忽略大小写
 */
function contains(string,substr,isIgnoreCase) {
    if(isIgnoreCase){
        string=string.toLowerCase();
        substr=substr.toLowerCase();
    }
    var startChar=substr.substring(0,1);
    var strLen=substr.length;
    for(var j=0;j<string.length-strLen+1;j++){
        if(string.charAt(j)==startChar){
            if (string.substring(j, j + strLen) == substr){
                return true;
            }
        }
    }
    return false;
}